# encoding: utf-8
from pyo import *

class Audio_Server:
    def __init__ (self):
        self.sndProcessing = []
        self.s= Server(sr=44100, buffersize=512, duplex=1)
        self.getAudioDrivers()
        self.s.setOutputDevice(self.defaultOutputIndex)
        
        self.s.boot()
        #self.s.start()

    def start(self):
        print("start is working")
        self.s.start()

    def stop(self):
        
        if self.s.getIsStarted():

            self.s.stop()
            print("stop is working")
            

    def setOut(self):
        self.output.out()
        return self

    def getSndProcessing(self):
        return self.sndProcessing
        
    def gui(self):
        self.s.gui(locals())

   
    def addSndProcessing(self, sndProcessing):#a verifier
        if sndProcessing == "delay":
            #methode out should be here
            self.sndProcessing.append(Dly().out())#should i make class of each fx or call the fx inside MFX
        elif sndProcessing == "distortion":
            self.sndProcessing.append(MFX().out())
        elif sndProcessing== "frequency shift":
            self.Processing.append(MFX().out())
        return self.sndProcessing[-1]

    def changeAudioDriver(self, driver): #***
        pos = self.outputDriverNames.index(driver)
        self.defaultOutputIndex = self.outputDriverIndexes[pos]
        self.stop()
        if self.s.getIsBooted():        
            self.s.shutdown()
        self.s.setOutputDevice(self.defaultOutputIndex)
        self.s.boot()

        
    def getAudioDrivers(self): #\\\
        self.outputDriverNames, self.outputDriverIndexes = pa_get_output_devices()
        #print self.outputDriverNames, self.outputIndexes
        self.defaultOutputIndex = pa_get_default_output()
        pos = self.outputDriverIndexes.index(self.defaultOutputIndex)
        self.defaultOutputName = self.outputDriverNames[pos]
        

    def getAudioDriverNames(self):
        return self.outputDriverNames, self.defaultOutputName
        #print self.outputDriverNames, self.defaultOutputName

class MFX(Audio_Server):

    def __init__(self):
        #Audio_Server.__init__(self)
        self._in=Input(chnl=0, mul=1, add=0)
        self._mix= Mixer (outs= 3, chnls=2, time=.05)
        #self._dly = Delay(self._mix[0], delay=0.78, feedback=0.5, maxdelay=1, mul=1, add=0)
        #self._dis= Disto(self._mix[1], drive=0.75, slope=0.50, mul=1, add=0)
        #self._frs= FreqShift(self._mix[2], shift=100, mul=1, add=0)
        #self._mix.addInput(0, self._in)
        #self._mix.addInput(1, self._frs)
        #self._mix.addInput(2, self._dly)
        #self._mix.addInput(3, self._dis)
        self.sndEffect=[]
        #self._mix.addInput(4, self._grain)

        #self.output= self._dly + self._dis + self._frs
    def addInput(self, _out,_in):
        self._mix.addInput(_out,_in)

    def setAmp(self, _ins, _outs, _vol):
        self._mix.setAmp(_ins, _outs, _vol)
        
    def addEffect(self,_in=0, sndEffect='delay'):#a verifier
        if sndEffect == "delay":
            #methode out should be here
            delai=Dly(self,_in)
            self.sndEffect.append(delai.out())#should i make class of each fx or call the fx inside MFX
        elif sndEffect == "distortion":
            disto=Dist(self,_in)
            self.sndEffect.append(disto.out())
        elif sndEffect== "frequency shift":
            freqShift = Frs(self, _in)
            self.sndEffect.append(freqShift.out())
        #return self.sndEffect[-1]

class Dly():
    def __init__(self,MFX,_out, delay=10, fb=.5, mul=.5):
        print("delay is working")
        self.name= "delay"
        self.fb= fb
        self.delay=delay
        self.mul=mul
        self.MFX=MFX
        self._dly = Delay(self.MFX._mix[_out], delay=self.delay, feedback=self.fb, maxdelay=1, mul=self.mul, add=0).out()
    def out(self):
        return self._dly
        #self.output= self._dly
        
class Dist():
    def __init__(self,MFX,_out, drive=0.99, slope=0.95, mul=1):
        print("Disto is working")
        self.name= "distortion"
        self.MFX=MFX
        self._disto=Disto(self.MFX._mix[_out], drive, slope, mul, add=0).out()
    def out(self):
        return self._disto
        #self.output= self._dly

class Frs():
    def __init__(self,MFX,_out, shift=104, mul=1):
        print("FreqShif is working")
        self.name= "frequency shift"
        self.MFX=MFX
        self._frs=FreqShift(self.MFX._mix[_out], shift, mul=1, add=0).out()
    def out(self):
        return self._frs
        

 
if __name__=="__main__" :
    import pyo
    #import srvr2
    Audio=Audio_Server()
    
    Master=MFX()
    
#    source1=SfPlayer(SNDS_PATH + "/transparent.aif", speed=1, loop=True, offset=0, interp=2, mul=1, add=0)
#    Master.addInput(0,Sig(source1, mul=100))
#    Master.addEffect(0,'frequency shift')
#    Master.setAmp(0,0,0.01)#setAmp(vin, vout, amp) 
#    #sig=Sig(Master._mix[0],mul=1000).out()
    
    Master.addInput(1,Sig(Master._in, mul=100))
    Master.addEffect(1,'delay')
    Master.setAmp(1,1,0.52)

    Master.addInput(2,Sig(Master._mix[1],mul=100))
    Master.addEffect(1,'distortion')
    Master.setAmp(1,2,0.1)
    


#    Master._in.out()

    Audio.gui()